import { SEVERE_RES, SEVERE_INFO, CHANGE_PRODUCT_COUNT, CHANGE_PRODUCT_HASSELETED, CHECK_ALL_SELECTED_PRODUCT } from './mutations-type'
import { api } from '@/service'

export default {
  getLogin({ commit, loginName, password }) {
    api.users.login(loginName, password).then(res => {
      console.log('*', res)
      commit(SEVERE_RES, { res })
    })
  },
  getShopInfo({ commit }) {
    api.users
      .shopInfo()
      .then(products => {
        console.log('******', products)
        commit(SEVERE_INFO, { products })
      })
      .catch(err => {
        console.log('err', err)
      })
  },
  /**
   * @description 增加或减少商品数量
   *
   * @param {Object} state Vuex state 对象
   * @param {String} produtId 商品编号
   * @param {String} type 操作类型|['add'：增加；'sub'：减少]
   */
  changeProductCount({ commit }, { productId, type }) {
    console.log('changeProductCount begin:', productId, type)

    commit(CHANGE_PRODUCT_COUNT, { productId, type })
  },
  hasSelectedProduct({ commit }, { hasSelected, index }) {
    commit(CHANGE_PRODUCT_HASSELETED, { hasSelected, index })
  },
  checkAllSelectedProduct({ commit }, allTrue) {
    commit(CHECK_ALL_SELECTED_PRODUCT, allTrue)
  },
}
